package cn.tedu.system.oa.notice.service.impl;

import cn.tedu.system.oa.notice.dao.repository.NoticeCacheRepository;
import cn.tedu.system.oa.notice.mapper.NoticeMapper;
import cn.tedu.system.oa.notice.pojo.param.NoticeAddParam;
import cn.tedu.system.oa.notice.pojo.dto.NoticeQuery;
import cn.tedu.system.oa.notice.pojo.entity.Notice;
import cn.tedu.system.oa.notice.service.NoticeService;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {
    
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private RedisTemplate<String, List<Notice>> redisTemplate;

    public List<Notice> getAllNotice(){
        ValueOperations<String,List<Notice>> stringOperations = redisTemplate.opsForValue();
        String noticeKey = "NOTICE_KEY";
        return stringOperations.get(noticeKey);
    }


    public void saveAllNotice(List<Notice> list) {
        String noticeKey = "NOTICE_KEY";
        redisTemplate.opsForValue().set(noticeKey,list);
    }
    
    @Override
    public List<Notice> selectNotice(NoticeQuery noticeQuery) {
        
        log.debug("查询公告列表业务:noticeQuery={}",noticeQuery);
        
        List<Notice> list = null;
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        
        if (noticeQuery != null) {
            log.info("有查询条件:{}", noticeQuery);
            if(noticeQuery.getId() != null){
                queryWrapper.eq("id",noticeQuery.getId());
            }
            if (noticeQuery.getNoticeTitle() != null) {
                queryWrapper.like("notice_title", noticeQuery.getNoticeTitle());
            }
            if (noticeQuery.getStatus() != null) {
                queryWrapper.like("status", noticeQuery.getStatus());
            }
            if (noticeQuery.getDeptId() != null) {
                queryWrapper.like("dept_id", noticeQuery.getDeptId());
            }
            if (noticeQuery.getStartTime() != null && noticeQuery.getEndTime() != null) {
                
                Date startDate = noticeQuery.getStartTime();
                Date endDate = noticeQuery.getEndTime();
                
                queryWrapper.between("create_time", startDate,endDate);
            }
            
            /**
             * 使用redis数据库优化数据查询
             */
            
            if (noticeQuery.getId() == null && noticeQuery.getNoticeTitle()==null && 
                noticeQuery.getStatus() == null && noticeQuery.getDeptId() == null &&
                noticeQuery.getStartTime() == null && noticeQuery.getEndTime() == null){
                log.debug("查询条件为空：{},使用redis查询全表",noticeQuery);
                List<Notice> allNotice = getAllNotice();
                if (!CollectionUtils.isEmpty(allNotice)){
                    log.debug("redis中有数据,直接返回:{}",allNotice);
                    return allNotice;
                }else {
                    log.debug("redis中数据:{},为空,查询数据库",allNotice);
                    list = noticeMapper.selectList(null);
                    log.debug("查询数据库结果存入redis中:{}",list);
                    saveAllNotice(list);
                    log.debug("redis存入成功");
                    return list;
                }
            }
            
            queryWrapper.orderByDesc("create_time");
            list = noticeMapper.selectList(queryWrapper);
        }
        return list;
    }

    @Override
    public void deleteNoticeById(Integer id) {
        log.debug("删除公告业务:{}",id);
        noticeMapper.deleteById(id);
        
        //数据库更新时,将其变更内容一并写入redis
        List<Notice> list = noticeMapper.selectList(null);
        saveAllNotice(list);
    }

    @Override
    public void saveNotice(NoticeAddParam noticeAddParam) {
        log.debug("新增/编辑公告业务:{}",noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        
        if (notice.getId() == null){
            notice.setCreateTime(new Date());
            noticeMapper.insert(notice);
        }else {
            UpdateWrapper<Notice> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",noticeAddParam.getId());
            noticeMapper.update(notice,wrapper);
        }
        
        //数据库更新时,将其变更内容一并写入redis
        List<Notice> list = noticeMapper.selectList(null);
        saveAllNotice(list);
    }
}
